//
//  DropButton.m
//  SohoNaru
//
//  Created by PnP_WhiteMacBook on 13. 6. 7..
//  Copyright (c) 2013년 ladaka Inc. All rights reserved.
//

#import "DropButton.h"
#import <QuartzCore/QuartzCore.h>
#import "WUtil.h"

#define kBgFrameSize 64
#define kThumbNailSize 58
#define kDeleteBtnSize 28
@implementation DropButton

/**
 이미지를 UIImage 기반으로 가져올 때 사용
 */
/*
 - (id)initWithFrame:(CGRect)frame thumbNail:(UIImage *)thumbNail
 {
 self = [super initWithFrame:frame];
 if (self) {
 
 self.backgroundColor = [UIColor clearColor];
 
 _isScrollEnable = YES;
 
 self.thumbNail = thumbNail;
 }
 return self;
 }
 */


/**
 이미지를 도큐먼트 디렉토리에서 가져올 때 사용
 */
- (id)initWithFrame:(CGRect)frame thumbNail:(NSString *)path {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        _isScrollEnable = YES;
        self.imgPath = path;
    }
    
    return self;
}

- (void)drawRect:(CGRect)rect
{
    //버튼 백그라운드 이미지
    UIImageView *ivBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ad_photo_list_bg.png"]];
    ivBg.frame = CGRectMake(0, 0, kBgFrameSize, kBgFrameSize);
    
    //    UIImage *image = self.thumbNail;
    //    NSLog(@"image:%@", image);
    
    UIImage *img = [UIImage imageWithContentsOfFile:self.imgPath];
    img = [WUtil resizeWithImage:img scaledToSize:CGSizeMake(img.size.width/4, img.size.height/4)];
    
    
    //드롭되는 이미지 크기 설정
    self.ivDropImg = [[UIImageView alloc] initWithFrame:CGRectMake(-3, -3, kBgFrameSize+6, kBgFrameSize+6)];
    self.ivDropImg.image = img;
    //    self.ivDropImg.image = self.thumbNail;
    
    self.ivImg = [[UIImageView alloc] initWithFrame:CGRectMake(3, 3, kThumbNailSize, kThumbNailSize)];
    self.ivImg.image = img;
    
    //삭제 버튼 설정
    self.btnRemove = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btnRemove setImage:[UIImage imageNamed:@"btn_photo_remove.png"] forState:UIControlStateNormal];
    [self.btnRemove setImage:[UIImage imageNamed:@"btn_photo_remove_ov.png"] forState:UIControlStateHighlighted];
    [self.btnRemove setFrame:CGRectMake(0, 0, kDeleteBtnSize, kDeleteBtnSize)];
    [self.btnRemove addTarget:self action:@selector(onClickDelete:) forControlEvents:UIControlEventTouchUpInside];
    self.btnRemove.tag = self.tag;
    
    self.ivDropImg.hidden = YES;
    [self addSubview:self.ivDropImg];
    [self addSubview:ivBg];
    [self addSubview:self.ivImg];
    [self addSubview:self.btnRemove];
}


- (void) layoutSubviews {
    
}


- (void) onClickDelete:(id)sender {
    UIButton *btn = (UIButton *)sender;
    self.tag = btn.tag;
    NSLog(@"tag:%i", self.tag);
    
    [self removeFromSuperview];
    if ([self.delegate respondsToSelector:@selector(didImageDelete:)]) {
        [self.delegate didImageDelete:self];
    }
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];
    
    if (_isScrollEnable != NO) {
        _originalPoint = self.ivDropImg.center;
        NSLog(@"originalP:%f, %f", _originalPoint.x, _originalPoint.y);
        
        self.svParent.scrollEnabled = NO;
        
        self.ivDropImg.hidden = NO;
        self.ivDropImg.alpha = 0.7;
        
        CGPoint newPoint = CGPointZero;
        newPoint = [self.mainView convertPoint:[[touches anyObject] locationInView:self.mainView] toView:self.mainView];
        NSLog(@"point : x=%f, y=%f", newPoint.x, newPoint.y);
        
        _originalOutsidePoint = newPoint;
        
        self.ivDropImg.center = newPoint;
        self.layer.borderColor = UIColorFromHexToRGB(0xD2202F).CGColor;
        self.layer.borderWidth = 2;
        
        [self.mainView addSubview:self.ivDropImg];
        [self.mainView bringSubviewToFront:self.ivDropImg];
        
        _isScrollEnable = NO;
    }
    
    if ([self.delegate respondsToSelector:@selector(didTouchDown:)]) {
        [self.delegate didTouchDown:self];
    }
}


- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
    
    [UIView beginAnimations:@"dropStart" context:nil];
    [UIView setAnimationDuration:.001];
    [UIView setAnimationBeginsFromCurrentState:YES];
    
    UITouch *touch = [touches anyObject];
    self.ivDropImg.center = [touch locationInView:self.mainView];
    
    [UIView commitAnimations];
    
    if ([self.delegate respondsToSelector:@selector(didViewInside:imgPath:touching:)]) {
        [self.delegate didViewInside:self.ivDropImg imgPath:self.imgPath touching:NO];
    }
}


- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    
    self.ivDropImg.hidden = YES;
    [self bringSubviewToFront:self.mainView];
    
    self.layer.borderWidth = 0;
    
    [UIView beginAnimations:@"dropEnd" context:nil];
    [UIView setAnimationDuration:0.4];
    [UIView setAnimationBeginsFromCurrentState:YES];
    
    
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    [UIView commitAnimations];
    self.svParent.scrollEnabled = YES;
    
    if ([self.delegate respondsToSelector:@selector(didTouchUp:)]) {
        [self.delegate didTouchUp:self];
    }
    
    if ([self.delegate respondsToSelector:@selector(didViewInside:imgPath:touching:)]) {
        [self.delegate didViewInside:self.ivDropImg imgPath:self.imgPath touching:YES];
    }
    self.ivDropImg.center = _originalPoint;
}

-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    
    if ([animationID isEqualToString:@"dropEnd"] && finished) {
        
        //        self.ivDropImg.center = _originalPoint;
        
        [self.svParent addSubview:self.ivDropImg];
        _isScrollEnable = YES;
    }
}



@end
